//
//  ShareViewController.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2020/5/18.
//  Copyright © 2020 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

class ShareViewController: AsModeViewController {
    
    var current:DSAppearance = .dark
    
    var customViews:[CuSetView] = []
    var titles:[String] = ["会议编号","会议密码","软件下载","网页参会"]
    
    var backColor:NSColor {
        return self.current == .dark ? color_gray(30):color_gray(230)
    }
    
    var backHoverColor:NSColor {
        return self.current == .dark ? color_gray(120):color_gray(220)
    }
    
    var mainColor:NSColor {
        return self.current == .dark ? color_gray(230):color_gray(40)
    }
    
    var cusSetColor:NSColor {
        return self.current == .dark ? color_gray(160):color_gray(120)
    }
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, kInvWinWidth, kInvWinHeight))
        view.wantsLayer = true
        let color = current == .dark ? color_gray(30):color_gray(230)
        view.layer?.backgroundColor = color.cgColor
        view.layer?.borderColor = color_gray(55).cgColor
        view.layer?.borderWidth = 1
        self.view = view
    }
    
    convenience init(appearance:DSAppearance = .dark) {
        self.init()
        self.current = appearance
        self.setupUI()
        self.loadData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
    }
    
    func setupUI() {
        self.view.addSubview(qrImagev)
        qrImagev.snp.makeConstraints { (make) in
            make.width.height.equalTo(200)
            make.top.equalTo(60)
            make.right.equalTo(-40)
        }
        
        
        self.view.addSubview(wechatLabel)
        wechatLabel.snp.makeConstraints { (make) in
            make.left.equalTo(qrImagev)
            make.height.equalTo(20)
            make.top.equalTo(30)
        }
        
        self.view.addSubview(content)
        content.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(30)
            make.right.equalTo(qrImagev.snp.left).offset(-20)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        self.content.addSubview(meetingTitle)
        meetingTitle.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(30)
            make.left.equalToSuperview()
        }
        
        self.content.addSubview(contentAccess)
        contentAccess.snp.makeConstraints { (make) in
            make.width.equalTo(310)
            make.left.equalToSuperview()
            make.top.equalTo(meetingTitle.snp.bottom).offset(20)
            make.bottom.equalToSuperview().offset(10)
        }
        
        self.customViews = self.initCusViews(inView: self.contentAccess, titles: self.titles)
        
        self.contentAccess.addSubview(copyIcon)
        copyIcon.snp.makeConstraints { (make) in
            make.width.equalTo(80)
            make.height.equalTo(30)
            make.left.equalTo(85)
            make.bottom.equalTo(-50)
        }
    }
    
    
    func initCusViews(inView:NSView,titles:[String]) -> [CuSetView] {
        var uCusViews:[CuSetView] = []
        for (index,value) in titles.enumerated() {
            var cusV:NSView?
            var cusSize:CGSize = CGSize.init(width: 220, height: 24)
            switch index {
            case 0:
                cusV = self.meetingId
            //case 1:
                //cusV = self.hostLab
            case 1:
                cusV = self.passwordLab
            case 2:
                cusV = self.appDownload
                cusSize = CGSize.init(width: cusSize.width, height: 40)
            case 3:
                cusV = self.webDownload
                cusSize = CGSize.init(width: cusSize.width, height: 40)
            default:
                break
            }
            
            let cusView = CuSetView.init(text: value+" ", cusView: cusV, cusSize: cusSize)
            cusView.titleWidth = 86
            cusView.title.textColor = cusSetColor
            cusView.title.alignment = .left
            cusView.title.font = NSFont.systemFont(ofSize: 12)
            inView.addSubview(cusView)
            uCusViews.append(cusView)
            cusView.snp.makeConstraints { (make) in
                make.width.equalTo(cusSize.width+cusView.titleWidth+30)
                make.height.equalTo(cusSize.height)
                make.left.equalToSuperview()
                if index == 0 {
                    make.top.equalTo(0)
                }else {
                    make.top.equalTo(uCusViews[index-1].snp.bottom).offset(5)
                }
                
            }
        }
        return uCusViews
    }
    
    
    
    func text() -> String {
        guard let ownid = Td.sh().params.userId else {
            return "ownid=nil"
        }
        let name = Td.sh().name
        let password = Td.sh().cloud.passwordCloud
        let newpwd = password=="" ? "无":password
        let cont = name+"邀请您参加视频会议".localized()+"\n"
        let cont3 = "会议号码:".localized()+ownid+"\n"
        //let cont4 = "会议时间:".localized()+"\n"
        let cont5 = "会议密码:".localized()+newpwd+"\n"
        let link1 = "客户端下载:".localized()+self.appDownload.stringValue+"\n"
        let link2 = "网页端列席地址:".localized()+self.webDownload.stringValue+"\n"
        let link3 = "小程序地址:".localized()+self.wechatDownload.stringValue+"\n"
        let tex = cont+cont3+cont5+link1+link2+link3
        return tex
    }

    
    
    func loadData() {
        let name = Td.sh().name
        self.meetingTitle.stringValue = name+" "+"邀请您参加视频会议".localized()
        self.hostLab.stringValue = name
        let password = Td.sh().cloud.passwordCloud
        self.passwordLab.stringValue = password == "" ? "无":password
        if let ownid = Td.sh().params.roomId {
            self.meetingId.stringValue = ownid
        }
        
    }
    
    lazy var meetingId: NSTextField = {
        let lab = NSTextField.init(string: "897132128")
        lab.backgroundColor = NSColor.clear
        lab.textColor = mainColor
        lab.maximumNumberOfLines = 1
        lab.font = NSFont.systemFont(ofSize: 12)
        lab.isBordered = false
        lab.isEditable = false
        lab.delegate = self
        lab.sizeToFit()
        return lab
    }()
    
    
    lazy var hostLab: NSTextField = {
        let lab = NSTextField.init(string: "")
        lab.backgroundColor = NSColor.clear
        lab.textColor = mainColor
        lab.maximumNumberOfLines = 1
        lab.font = NSFont.systemFont(ofSize: 12)
        lab.isBordered = false
        lab.isEditable = false
        lab.delegate = self
        lab.sizeToFit()
        return lab
    }()
    
    lazy var passwordLab: NSTextField = {
        let lab = NSTextField.init(string: "")
        lab.backgroundColor = NSColor.clear
        lab.textColor = mainColor
        lab.maximumNumberOfLines = 1
        lab.font = NSFont.systemFont(ofSize: 12)
        lab.isBordered = false
        lab.isEditable = false
        lab.delegate = self
        lab.sizeToFit()
        return lab
    }()
    
    lazy var appDownload: NSTextField = {
        let string = "http://www.1meeting.cn/encyclopedialist?id=93&level=2"
        let attString = NSAttributedString.init(string: string, attributes: [NSAttributedString.Key.link:string])
        let lab = NSTextField.init(labelWithAttributedString: attString)
        lab.backgroundColor = NSColor.clear
        lab.maximumNumberOfLines = 2
        lab.font = NSFont.systemFont(ofSize: 12)
        lab.isBordered = false
        lab.isEditable = false
        lab.delegate = self
        lab.sizeToFit()
        return lab
    }()
    
    lazy var webDownload: NSTextField = {
        let string = "https://www.1meeting.cn/process/videomeeting/onemeetingLiventrance"
        let attString = NSAttributedString.init(string: string, attributes: [NSAttributedString.Key.link:string])
        let lab = NSTextField.init(labelWithAttributedString: attString)
        lab.backgroundColor = NSColor.clear
        lab.maximumNumberOfLines = 2
        lab.font = NSFont.systemFont(ofSize: 12)
        lab.isBordered = false
        lab.isEditable = false
        lab.delegate = self
        lab.sizeToFit()
        return lab
    }()
    
    lazy var wechatDownload: NSTextField = {
        let string = "https://www.1meeting.cn/process/videomeeting/onemeetingLiventrance"
        let attString = NSAttributedString.init(string: string, attributes: [NSAttributedString.Key.link:string])
        let lab = NSTextField.init(labelWithAttributedString: attString)
        lab.backgroundColor = NSColor.clear
        lab.maximumNumberOfLines = 2
        lab.font = NSFont.systemFont(ofSize: 12)
        lab.isBordered = false
        lab.isEditable = false
        lab.delegate = self
        lab.sizeToFit()
        return lab
    }()
    
    lazy var wechatLabel: NSLabel = {
        let lab = NSLabel.label(title: "小程序参会", textColor: color_gray(255), font: NSFont.systemFont(ofSize: 14))
        lab.sizeToFit()
        return lab
    }()
    
    lazy var invateView: InvateLinkView = {
        let view = InvateLinkView.init(appearance: self.current) {
            self.copyBtnAction()
        }
        return view
    }()
    
    lazy var copyIcon: KIcon = {
        let btn = KIcon.init(data: KIconGroup.init(title: "复制信息", image: "icon_invate_copy"))
        btn.imagePosition = .left
        btn.title.textColor = color_main140
        btn.bgView.layer?.backgroundColor = self.backColor.cgColor
        btn.tap = { view in
            self.touchAction()
        }
        btn.status = { view in
            switch view {
            case .normal,.selected:
                btn.bgView.layer?.backgroundColor = self.backColor.cgColor
            case .hover,.highlight:
                btn.bgView.layer?.backgroundColor = self.backHoverColor.cgColor
            }
        }
        btn.wantsLayer = true
        btn.layer?.cornerRadius = 3
        btn.layer?.masksToBounds = true
        return btn
    }()
    
    
    lazy var qrImagev: NSImageView = {
        let imageV = NSImageView.init(frame: CGRect.zero)
        imageV.image = QRManager.createQRImage(message:"https://www.1meeting.cn/process/videomeeting/onemeetingLiventrance", size: NSSize.init(width: 200, height: 200), backgroundColor: .white, foregroundColor: .black, fillImage: nil)
        return imageV
    }()
    
    lazy var meetingTitle: NSTextField = {
        let lab = NSTextField.init(string: "邀请您参加视频会议")
        lab.backgroundColor = NSColor.clear
        lab.textColor = mainColor
        lab.maximumNumberOfLines = 1
        lab.font = NSFont.systemFont(ofSize: 20)
        lab.isBordered = false
        lab.isEditable = false
        lab.delegate = self
        lab.sizeToFit()
        return lab
    }()
    
    lazy var content: NSView = {
        let view = NSView.init()
        return view
    }()
    
    lazy var contentAccess: NSView = {
        let view = NSView.init()
        return view
    }()
    
    @objc func copyBtnAction() {
        NSPasteboard.general.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
        NSPasteboard.general.setString(self.text(), forType: NSPasteboard.PasteboardType.string)
        self.view.makeToast(message: "复制成功".localized(),isImage:true)
    }
    
    @objc func touchAction() {
        self.copyBtnAction()
    }
}


extension ShareViewController:NSTextFieldDelegate {
    override func performKeyEquivalent(with event: NSEvent) -> Bool {
        
        let modifierkeys = event.modifierFlags.intersection(.deviceIndependentFlagsMask)
        let key = event.characters ?? ""
        /// 点击 esc 取消焦点
        if modifierkeys.rawValue == 0 && key == "\u{1B}" {
            self.view.window?.makeFirstResponder(nil)
        }
        // command + shift + z 还原
        if modifierkeys == [.command, .shift] && key == "z" {
            self.view.window?.firstResponder?.undoManager?.redo()
            return true
        }
        if modifierkeys != .command {
            return super.performKeyEquivalent(with: event)
        }
        switch key {
        case "a":  // 撤消
            return NSApp.sendAction(#selector(NSText.selectAll(_:)), to: self.view.window?.firstResponder, from: self)
        case "c":  // 复制
            return NSApp.sendAction(#selector(NSText.copy(_:)), to: self.view.window?.firstResponder, from: self)
        case "v":  // 粘贴
            return NSApp.sendAction(#selector(NSText.paste(_:)), to: self.view.window?.firstResponder, from: self)
        case "x":  // 剪切
            return NSApp.sendAction(#selector(NSText.cut(_:)), to: self.view.window?.firstResponder, from: self)
        case "z":  // 撤消
            self.view.window?.firstResponder?.undoManager?.undo()
            return true
        default:
            return super.performKeyEquivalent(with: event)
        }
    }
    
}
